/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gtree;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import no.geosoft.cc.graphics.*;

/**
 *
 * @author Gustavo
 */
public class DrawTreePanel extends JPanel implements GInteraction {

    private GScene scene_;
    private Tree tree;
    private JFrame parent;

    public void setTree(Tree tree)
    {
        this.tree = tree;
           drawTree();
    }

    public void setParent(JFrame parent) {
        this.parent = parent;
    }

 

    public DrawTreePanel() {
        super();
    }

    private ArrayList<GObject> recAddNode(GObject parent, Node n, ArrayList<GObject> array) {
        array.add(new gNode(parent, n.getElement(), n.getX(), n.getY()));
       // System.out.println("ADICIONADO " + n.getElement() + " EM X-Y " + n.getX() + "-" + n.getY());
        if (n.getChildren().size() > 0) {
            GObject parent_ = array.get(array.size() - 1);
            for (int i = 0; i < n.getChildren().size(); i++) {
                recAddNode(parent_, n.getChildren().get(i), array);
            }
        }
        return array;
    }

    @Override
    public void event(GScene scene, int event, int x, int y) {
        if (event == GWindow.BUTTON1_UP
                || event == GWindow.BUTTON2_UP) {
            boolean isSelected = event == GWindow.BUTTON1_UP;

            GSegment selectedSegment = scene_.findSegment(x, y);
            if (selectedSegment == null) {
                return;
            }

            GStyle style = selectedSegment.getOwner().getStyle();
            if (style == null) {
                return;
            }

            if (isSelected) {
                style.setBackgroundColor(new Color((float) Math.random(),
                        (float) Math.random(),
                        (float) Math.random()));
            } else {
                style.unsetBackgroundColor();
            }

            scene_.refresh();
        }
    }

    private Tree createTree() {
        Tree tree = new Tree("A");
        Position a = tree.root();
        Position b = tree.addChild(a, "B");
        Position c = tree.addChild(a, "C");
        Position d = tree.addChild(b, "D");
        Position f = tree.addChild(d, "F");
        Position g = tree.addChild(f, "G");

        tree.addChild(d, "FD1");
        tree.addChild(d, "FD2");
        tree.addChild(c, "FC1");
        tree.addChild(c, "FC2");
        tree.addChild(d, "FD3");
        tree.addChild(d, "FD4");
        tree.addChild(d, "FD5");
        tree.addChild(c, "FC3");
        tree.addChild(c, "FC4");
        tree.addChild(a, "FA1");
        tree.addChild(a, "FA2");
        tree.addChild(a, "FA3");
        Position fg = tree.addChild(g, "FG");
        tree.addChild(f, "FF");
        return tree;
    }

    

    public void drawTree() {

        // Create the GUI
        this.setLayout(new BorderLayout());

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(new JLabel("Button 1 to select color, button 2 to unselect"));

        JPanel graphicsPanel = new JPanel();
        this.add(buttonPanel, BorderLayout.NORTH);

        // Create the graphic canvas
        GWindow window = new GWindow();
        parent.add(window.getCanvas(), BorderLayout.CENTER);

        //CREATE TREE
      //  Tree tree = createTree();

        // Create scene with default viewport and world extent settings
        scene_ = new GScene(window, "Tree");
        /*
         * double w0[] = {0.0, 1200.0, 0.0}; double w1[] = {1200.0, 1200.0,
         * 0.0}; double w2[] = {0.0, 0.0, 0.0}; scene_.setWorldExtent(w0, w1,
         * w2);
         */
//        scene_.setWorldExtent(0, 0, tree.getDimensionX() + 100, tree.getDimensionY() + 100);

        GStyle style = new GStyle();
        style.setForegroundColor(new Color(0, 0, 0));
        style.setBackgroundColor(new Color(255, 255, 255));
        style.setFont(new Font("Dialog", Font.BOLD, 14));
        scene_.setStyle(style);



        ArrayList<GObject> objects = new ArrayList<GObject>();

        recAddNode(scene_, ((Node) tree.root()), objects);

        setSize((int) tree.getDimensionX(), (int) tree.getDimensionY());
        setVisible(true);

        window.startInteraction(this);
    }
}